package net.sf.rose.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

public class ImageUtils {

    public static void main(String[] args) {
        // ImageUtils.formatImageSize(new File("d:\\", "test.jpg"), 780, 1080);
        ImageUtils.addText(new File("d:\\qrcode.jpg"), new File("d:\\2.jpg"), "技术部监制");
    }

    /**
     * 按比例修改图片尺寸，如果超过最大宽度或最大高度就按比例缩小
     * @param file      - 图片文件
     * @param maxWidth  - 文件宽度
     * @param maxHeight - 文件高度
     * @return 图片缩小了返回true
     */
    public static boolean formatImageSize(File file, int maxWidth, int maxHeight) {
        try {
            Image img = ImageIO.read(file);
            // 构造Image对象
            int w = img.getWidth(null); // 得到源图宽
            int h = img.getHeight(null); // 得到源图长
            if (w <= maxWidth && h <= maxHeight) {
                return false;
            } else {
                if (w > maxWidth) {
                    double n = (double) maxWidth / (double) w;
                    h = (int) (n * h);
                    w = maxWidth;
                }
                if (h > maxHeight) {
                    double n = (double) maxHeight / (double) h;
                    w = (int) (n * w);
                    h = maxHeight;
                }
            }
            BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(img, 0, 0, w, h, null);
            OutputStream out = new FileOutputStream(file);
            ImageIO.write(tag, "jpg", out);
            out.close();
            return true;
        } catch (IOException e) {
            throw new RoseRuntimeException(e);
        }
    }

    /**
     * 按比例修改图片尺寸，如果超过最大宽度或最大高度就按比例缩小
     * @param file      - 图片文件
     * @param maxWidth  - 文件宽度
     * @param maxHeight - 文件高度
     * @return 图片缩小了返回true
     */
    public static boolean addText(File source, File target, String text) {
        try {
            Image img = ImageIO.read(source);
            int w = img.getWidth(null); // 得到源图宽
            int h = img.getHeight(null); // 得到源图长
            BufferedImage bi = new BufferedImage(w, h + 25, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bi.createGraphics();
            g2.setBackground(Color.WHITE);
            g2.clearRect(0, 0, w, h + 25);
            g2.drawImage(img, 0, 0, w, h, null);

            /** 设置生成图片的文字样式 * */
            Font font = new Font("黑体", Font.BOLD, 25);
            g2.setFont(font);
            g2.setPaint(Color.BLACK);

            /** 设置字体在图片中的位置 在这里是居中* */
            FontRenderContext context = g2.getFontRenderContext();
            Rectangle2D bounds = font.getStringBounds(text, context);
            double x = (w - bounds.getWidth()) / 2;
            //double y = (height - bounds.getHeight()) / 2; //Y轴居中
            double y = (h + 25 - bounds.getHeight());
            double ascent = -bounds.getY();
            double baseY = y + ascent;

            /** 防止生成的文字带有锯齿 * */
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            /** 在图片上生成文字 * */
            g2.drawString(text, (int) x, (int) baseY);
            ImageIO.write(bi, "jpg", target);
            return true;
        } catch (IOException e) {
            throw new RoseRuntimeException(e);
        }
    }
}